<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chartInstance: null,
      allData: [
        {
          name: "广东",
          value: 230,
        },
        {
          name: "福建",
          value: 214,
        },
        {
          name: "浙江",
          value: 203,
        },
        {
          name: "上海",
          value: 310,
        },
        {
          name: "北京",
          value: 289,
        },
        {
          name: "江苏",
          value: 207,
        },
        {
          name: "四川",
          value: 189,
        },
        {
          name: "重庆",
          value: 195,
        },
        {
          name: "陕西",
          value: 160,
        },
        {
          name: "湖南",
          value: 140,
        },
        {
          name: "河北",
          value: 170,
        },
        {
          name: "辽宁",
          value: 106,
        },
        {
          name: "湖北",
          value: 120,
        },
        {
          name: "江西",
          value: 99,
        },
        {
          name: "天津",
          value: 107,
        },
        {
          name: "吉林",
          value: 143,
        },
        {
          name: "青海",
          value: 65,
        },
        {
          name: "山东",
          value: 166,
        },
        {
          name: "山西",
          value: 134,
        },
        {
          name: "云南",
          value: 87,
        },
        {
          name: "安徽",
          value: 79,
        },
      ],
      startValue: 0, // 区域缩放的起点值
      endValue: 9, // 区域缩放的终点值
      timerId: null, // 定时器的标识
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timerId);
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, this.theme);
      const initOption = {
        backgroundColor: "#ffffff",
        //设置图标的标题
        title: {
          text: "▎ 地区销售排行",
          left: 20,
          top: 0,
        },
        //设置图表的位置
        grid: {
          top: "18%",
          left: "3%",
          right: "5%",
          bottom: "2%",
          containLabel: true,
        },
        //工具提示
        tooltip: {
          show: true,
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            type: "bar",
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData() {
      // 获取服务器的数据, 对this.allData进行赋值之后, 调用updateChart方法更新图表
      // const{data:ret} = await this.$axios.get('rank')
      // this.allData = ret
      // 对allData里面的每一个元素进行排序, 从大到小进行
      this.allData.sort((a, b) => {
        return b.value - a.value;
      });
      // console.log(ret);
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      // 处理图表需要的数据
      //所有省份形成的数组
      const provinceArr = this.allData.map((item) => {
        return item.name;
      });
      //所有省份销售金额
      const valueArr = this.allData.map((item) => {
        return item.value;
      });
      const dataOption = {
        xAxis: {
          data: provinceArr,
        },
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            type: "bar",
            barWidth: 20,
            data: valueArr,
            itemStyle: {
              barBorderRadius: [10, 10, 0, 0],
              color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                // 百分之0状态之下的颜色值
                {
                  offset: 0,
                  color: "#03cad7",
                },
                // 百分之100状态之下的颜色值
                {
                  offset: 1,
                  color: "#fb9678",
                },
              ]),
            },
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
          left: 10,
          top: 6,
        },
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 2000);
    },
  },
};
</script>

<style>
</style>